

#import "ContactInfoHTMLReader.h"
#import "IDominoAppDelegate.h"

@implementation ContactInfoHTMLReader

@synthesize currentPersonObject = _currentPersonObject;
@synthesize contentOfCurrentPersonProperty = _contentOfCurrentPersonProperty;
@synthesize currentAttributeTitle = _currentAttributeTitle;

- (ContactInfoHTMLReader*)initWithPerson: (Person*) person {
	self.currentPersonObject=person;
	return self;
}
- (void)parserDidStartDocument:(NSXMLParser *)parser
{
}

- (BOOL)parseXMLFileFromData:(NSData *)data parseError:(NSError **)error
{	
    NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
    // Set self as the delegate of the parser so that it will receive the parser delegate methods callbacks.
    [parser setDelegate:self];
    // Depending on the XML document you're parsing, you may want to enable these features of NSXMLParser.
    [parser setShouldProcessNamespaces:NO];
    [parser setShouldReportNamespacePrefixes:NO];
    [parser setShouldResolveExternalEntities:NO];
    
    [parser parse];
    
    NSError *parseError = [parser parserError];
    if (parseError && error) {
        *error = parseError;
		[parser release];
		return NO;
    }
    
    [parser release];
	return YES;
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    if (qName) {
        elementName = qName;
    }  
    if ([elementName isEqualToString:@"input"]) {
		if([[attributeDict valueForKey:@"name"] isEqualToString:@"CompanyName"])
			[self.currentPersonObject setCompany :[attributeDict valueForKey:@"value"]];
		else if([[attributeDict valueForKey:@"name"] isEqualToString:@"JobTitle"])
			[self.currentPersonObject setJobTitle:[attributeDict valueForKey:@"value"]];
		else if([[attributeDict valueForKey:@"name"] isEqualToString:@"WebSite"])
			[self.currentPersonObject setPersonURL :[attributeDict valueForKey:@"value"]];
		else if([[attributeDict valueForKey:@"name"] isEqualToString:@"OfficeCountry"])
			[self.currentPersonObject setCountry:[attributeDict valueForKey:@"value"]];
		else if([[attributeDict valueForKey:@"name"] isEqualToString:@"OfficeZip"])
			[self.currentPersonObject setZip:[attributeDict valueForKey:@"value"]];
		else if([[attributeDict valueForKey:@"name"] isEqualToString:@"OfficeState"])
			[self.currentPersonObject setState:[attributeDict valueForKey:@"value"]];
		else if([[attributeDict valueForKey:@"name"] isEqualToString:@"OfficeCity"])
			[self.currentPersonObject setCity:[attributeDict valueForKey:@"value"]];
		else if([[attributeDict valueForKey:@"name"] isEqualToString:@"OfficeStreetAddress"])
			[self.currentPersonObject setStreet:[attributeDict valueForKey:@"value"]];
		else if([[attributeDict valueForKey:@"name"] isEqualToString:@"CellPhoneNumber"])
			[self.currentPersonObject setMobile:[attributeDict valueForKey:@"value"]];
		else if([[attributeDict valueForKey:@"name"] isEqualToString:@"Birthday"])
			[self.currentPersonObject setBirthday:[attributeDict valueForKey:@"value"]];
		else if([[attributeDict valueForKey:@"name"] isEqualToString:@"FullNameInput"]){
			NSString* fullName=[attributeDict valueForKey:@"value"];
			NSRange range=[fullName rangeOfString:@", "];
			if(range.location!=NSNotFound){
				[self.currentPersonObject setSuffix:[fullName substringFromIndex:range.location]];
				fullName=[fullName substringToIndex:range.location];
			}
			
			NSArray* nameArray=[fullName componentsSeparatedByString:@" "];
			if([nameArray indexOfObject:self.currentPersonObject.firstName]==1){
				[self.currentPersonObject setPrefix:[ nameArray  objectAtIndex:0]];
			}
			
		}
		self.contentOfCurrentPersonProperty=nil;
	}
	else if ([elementName isEqualToString:@"textarea"]) {
		if([[attributeDict valueForKey:@"name"] isEqualToString:@"OfficePhoneNumber"]){
			self.currentAttributeTitle=@"OfficePhoneNumber";
		}
		else if([[attributeDict valueForKey:@"name"] isEqualToString:@"OfficeFAXPhoneNumber"]){
			self.currentAttributeTitle=@"OfficeFAXPhoneNumber";
		}
		else if([[attributeDict valueForKey:@"name"] isEqualToString:@"Department"]){
			self.currentAttributeTitle=@"Department";
		}
		else if([[attributeDict valueForKey:@"name"] isEqualToString:@"PhoneNumber_6"]){
			self.currentAttributeTitle=@"PhoneNumber_6";
		}
		else if([[attributeDict valueForKey:@"name"] isEqualToString:@"HomeFAXPhoneNumber"]){
			self.currentAttributeTitle=@"HomeFAXPhoneNumber";
		}
		else if([[attributeDict valueForKey:@"name"] isEqualToString:@"FullName"]){
			self.currentAttributeTitle=@"FullName";
		}
		else{
			self.currentAttributeTitle=nil;
		}
		self.contentOfCurrentPersonProperty=[NSMutableString stringWithCapacity:30];
	}else{
		self.contentOfCurrentPersonProperty=nil;
	}
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{     
    if (qName) {
        elementName = qName;
    }
    
    if ([elementName isEqualToString:@"textarea"]) {
		if([self.currentAttributeTitle isEqualToString:@"OfficePhoneNumber"]){
			[self.currentPersonObject setWorkPhone:self.contentOfCurrentPersonProperty];
		}
		else if([self.currentAttributeTitle isEqualToString:@"OfficeFAXPhoneNumber"]){
			[self.currentPersonObject setWorkFax:self.contentOfCurrentPersonProperty];
		}
		else if([self.currentAttributeTitle isEqualToString:@"Department"]){
			[self.currentPersonObject setDepartment:self.contentOfCurrentPersonProperty];;
		}
		else if([self.currentAttributeTitle isEqualToString:@"PhoneNumber_6"]){
			[self.currentPersonObject setWorkPager:self.contentOfCurrentPersonProperty];;
		}
		else if([self.currentAttributeTitle isEqualToString:@"HomeFAXPhoneNumber"]){
			[self.currentPersonObject setHomeFax:self.contentOfCurrentPersonProperty];;
		}
		else if([self.currentAttributeTitle isEqualToString:@"FullName"]){
			NSString* fullName=self.contentOfCurrentPersonProperty;
			int loc=[fullName rangeOfString:@"\n" options:NSBackwardsSearch].location;
			if(loc!=NSNotFound)
				[self.currentPersonObject setEmail:[fullName substringFromIndex:loc+1]];
			NSArray* nameArray=[fullName componentsSeparatedByString:@" "];
			if([nameArray count]>1 && (![self.currentPersonObject.lastName isEqualToString: [nameArray objectAtIndex:1]]) 
			   && (![self.currentPersonObject.firstName hasPrefix:fullName]) && (![self.currentPersonObject.lastName hasPrefix:fullName]))
				[self.currentPersonObject setMiddleName:[nameArray objectAtIndex:1]];
		}
	}
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    if (self.contentOfCurrentPersonProperty) {
		//self.contentOfCurrentMailProperty=string;
        // If the current element is one whose content we care about, append 'string'
        // to the property that holds the content of the current element.
        [self.contentOfCurrentPersonProperty appendString:string];
    }
	
}

@end


